'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 对手信息
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var Code = require('../../../../consts/code');
var battleModel = require('../../../../models/battleModel');
var rivalService = require('../../../../services/rivalService');
var roleService = require('../../../../services/roleService');
var skillService = require('../../../../services/skillService.js');
var heroConf = require('../../../../config/heroConf');
var PlayerManager = require('../../../../libs/playerManager');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var rival_role_id = msg.rival_role_id;
    if (!rival_role_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var rival_info;
    var player_manager = new PlayerManager(rival_role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.getModel(['skill'], cb);
            },
            function (cb) {
                skillService.migrateSkill(player_manager);

                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'battle', 'build', 'soldier', 'pvp', 'accrued'], cb);
            },
            function (cb) {
                rivalService.getInfo(player_manager, cb);
            },
            function (info, cb) {
                rival_info = info;
                var pvp_model = player_manager.pvp;
                pvp_model.rival_id = rival_role_id;
                pvp_model.rival_gold = rival_info.gold;
                pvp_model.rival_food = rival_info.food;
                cb();
            },
            function (cb) {
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    rival: rival_info
                }
            });
        }
    );
};
